
## convenience function
## takes names as characters
## prepares 2 Rnw files (figure and table) and then knits them
## should be called in a chunk with results='asis'
## after the named data.frame is prepared
## 
mk.lm.tex <- function(chunk.name, df.name, caption) {
    ## call data.frame by name, must be loaded and contain x and y vars
    plot.string <- sprintf(
"
<<plot_%s, fig.width=6, fig.height=4, message=F, echo=F, fig.cap='%s'>>=
    xyplot(y ~ x, %s, type=c('p','g','r'))
@
", 
    chunk.name, caption, df.name
    )
    ## write xtable results as latex
    table.string <- sprintf(
"
<<lm_%s, results='asis', echo=F>>=
    my.lm.summary <- summary(lm(y ~ x, %s))
    print(
        xtable(
            my.lm.summary, digits=3,
            caption=sprintf('Linear regression.  $R^2$ = %%0.2f', my.lm.summary$adj.r.sq)
        )
    )
@
"
    , chunk.name, df.name
    )
    ## file name of *these* code chunks
    .file <- sprintf('output-%s.Rnw', chunk.name)
    ## write everything out
    cat(plot.string, file=.file)
    cat(table.string, file=.file, append=T)
    ## build the tex file
    knit(.file, quiet=T)
    ## finally, write the \include command to the master Rnw/tex file
    cat(sprintf("\\include{output-%s}\n", chunk.name))
}
